#ifndef   H_A2TR
#define   H_A2TR
#define   A2TR_H

#ifndef H_A2SYSDEF
# include "a2sysdef.h"
#endif

#ifndef H_A2NIKA
# ifndef H_A2DIO
#  include "a2dio.h"
# endif
#endif

#ifndef H_A2TRDEF
# include "a2trdef.h"
#endif

/*---------------------------*/
/*--- transaction control ---*/
/*---------------------------*/
/*--- defined in A2DIO.C ----*/
/*---------------------------*/
#define NKNtBegin a2TransBegin
#define NKNtEnd   a2TransEnd
#define NKNtBreak a2TransBreak

#ifdef __cplusplus
extern "C" {
#endif

A2EXT int A2EXP a2TransBegin (int type, unsigned time, ...);
A2EXT int A2PAS a2vTransBegin(int type, unsigned time, char *pmodes,TREE *pa[]);
A2EXT int A2PAS a2TransEnd (void *pt);
A2EXT int A2PAS a2TransBreak (void *pt);
A2EXT int A2PAS a2TransList (char *modes, TREE *ptrees[]);

#ifdef __cplusplus
}
#endif

#ifndef   A2DIO_SOURCE

/*------------------------------------------------------------------*/
/*  DATA  STORAGE  SYSTEM  CODES  OF THE  PSEUDOSTATES  AND  ERRORS */
/*------------------- pseudostates ---------------------------------*/
#define ET_SM -1                                     /* empty level */
#define ET_SA -2                               /* no current vertex */
#define ET_SB -3                                 /* level beginning */
#define ET_SE -4                                       /* level end */
#define ET_SP -5                                /* protected vertex */
#define ET_SV -6                            /* vertical pseudostate */
/*------------------- insignificant errors -------------------------*/
#define ET_RMK -7                              /* reading empty key */
#define ET_RMD -8                            /* reading empty value */
#define ET_FERR -8         /* boundary code of insignificant errors */
/*=================== serious errors ===============================*/
/*------------------- tree opening ---------------------------------*/
#define ET_OR -15                  /* invalid format of root record */
#define ET_OU -16                     /* erroneous set of arguments */
#define ET_OM -17                              /* not enough memory */
#define ET_OE -18                                     /* empty file */
/*------------------- tree closing ---------------------------------*/
#define ET_CT  -19                    /* erroneous pointer to table */
/*------------------- operations of movement -----------------------*/
#define ET_MAR -20       /* improper movement or action in the root */
#define ET_MS  -21            /* invalid movement or operation code */
#define ET_LN  -22                       /* invalid number of level */
#define ET_TRC -23                     /* transaction is not opened */
#define ET_TRR -24                  /* transaction for reading only */
#define ET_MST -26              /* movement down in terminal vertex */
#define ET_MM  -27                             /* not enough memory */
#define ET_MK  -28                              /* invalid key type */
#define ET_LKT -29   /* the key type is inconsistent for this level */
#define ET_MD  -30                            /* invalid value type */
/*------------------- r/w operations -------------------------------*/
#define ET_LK  -31                          /* text key is too long */
#define ET_LD  -32                        /* text value is too long */
#define ET_KRD -34  /* operation may violate key order in the level */
#define ET_ANT -35            /* reading value in structural vertex */
#define ET_ANV -36           /* vertex with this key already exists */
#define ET_AND -37           /* value already exists in this vertex */
#define ET_AEV -38                        /* no vertex for deleting */
#define ET_AED -39                         /* no value for deleting */
#define ET_TRO -40                   /* transaction wasn't finished */
#define ET_NOK -41                     /* writing value without key */
#define ET_RO  -42       /* !!!=(W17)=!!!   write to READ ONLY tree */
#define ET_BS  -43            /* different sizes of blocks in files */
#define ET_RC  -44                    /* improper copying or moving */
#define ET_PC  -45               /* erroneous pointer to copy table */
#define ET_AC  -46                        /* invalid operation code */
#define ET_INT -47 /* internal system error or in the state of base */
#define ER_DA  DA_ERR             /* addition to the da error codes */


/*   MACROOPERATIONS  FOR  DATA  MANIPULATION  */

#if (defined(__FOR_WINDOWS__) || defined(__FOR_WIN32__)) && defined(H_A2NIKA)
# define TREESKD(e,p,m,o, kt,k,dt,d) {*e=TreeSKD(p, m, o, kt, k, dt, d);}
# define TREESK(err,ptr,m1,a,kt1,k1) {*err=TreeSK( ptr, m1, a, kt1, k1);}
# define TREESD(err,ptr,m1,a,vt1,v1) {*err=TreeSD( ptr, m1, a, vt1, v1);}
# define TREES(err,ptr,m1,a)         {*err=TreeS( ptr, m1, a);          }
# define TREECPY(err,mv,ptr1,ptr2)   {*err=TreeCPY( mv, ptr1, &ptr2);   }
# define TREEDUP(err,ptr1,ptr2)      {*err=TreeDUP( ptr1, &ptr2);       }
# define TREEDLT(err,ptr1,ptr2)      {*err=TreeDLT( ptr1, &ptr2);       }
# define TREESAV(err,pt)             {*err=TreeSAV( pt );               }
# define TREERST(err,pt)             {*err=TreeRST( pt );               }
# define TREECP(err,ptr,fc)          {*err=TreeCP(ptr);                 }
#else
# define TREESKD(e,p,m,a,kt,k,dt,d) {*e=a2tr1c(p,m,a, kt,k, dt,d, 3);}
# define TREESK(e,p,m,a,kt,k) {*e=a2tr1c(p, m, a, kt, k, 0, NULL, 1);}
# define TREESD(e,p,m,a,dt,d) {*e=a2tr1c(p, m, a, 0, NULL, dt, d, 2);}
# define TREES(err,ptr,m1,a) {*err=a2tr1c(ptr, m1,a, 0,NULL, 0,NULL, 0);}
# define TREECPY(e,mv,ptr1,ptr2) {*e=a2tr1c(ptr1,S_O,mv,0,NULL,0,&ptr2,2);}
# define TREEDUP(err,ptr1,ptr2) {*err=a2tr1c(ptr1,S_O,D_P,0,NULL,0,&ptr2,2);}
# define TREEDLT(err,ptr1,ptr2) {*err=a2tr1c(ptr1,S_O,R_C,0,NULL,0,&ptr2,2);}
# define TREESAV(err,pt) {*err=a2tr1c(pt, S_O,S_P, 0,NULL, 0,NULL, 0);}
# define TREERST(err,pt) {*err=a2tr1c(pt, S_O,R_P, 0,NULL, 0,NULL, 0);}
# define TREECP(err,ptr,fc) a2dacp(err, ptr->pdf, fc)
#endif
#define TREESCPY TREECPY
#define TREESDUP TREEDUP
#define TREESDLT TREEDLT
#define TREESSAV TREESAV
#define TREESRST TREERST

/* ACCESS TO THE CONTROL BLOCK DATA AND ENVIRONMENT PARAMETERS */

/*---------------- set dense writing regime -----------------*/
#define TREEPACKP TreePACKP
#define TreePACKP(ptree,t) ptree->pack=t
/*---------------- read dense writing regime ----------------*/
#define TREEPACK TreePACK
#define TreePACK(ptree) (ptree->pack & 1)
/*-------------------- set trace level ----------------------*/
#define TREETRACEP TreeTRACEP
#define TreeTRACEP(ptree,t,st)                     \
    strncpy(ptree->trid,st,sizeof(ptree->trid)-1), \
    ptree->trc = t;
/*------------------- read trace level ----------------------*/
#define TREETRACE TreeTRACE
#define TreeTRACE(ptree) (ptree->trc)
/*------------------- set message level ---------------------*/
#define TREEERMSGP TreeERMSGP
#define TreeERMSGP(ptree,t) ptree->emsg=t
/*------------------ read message level ---------------------*/
#define TREEERMSG TreeERMSG
#define TreeERMSG(ptree) (ptree->emsg)
/*------------- level number from tree root -----------------*/
#define TREELVL TreeLVL
#define TreeLVL(ptree) (ptree->lvl)
/*----------- level number from stack header ----------------*/
#define TREEGLVL TreeGLVL
#define TreeGLVL(ptree) (ptree->gllvl)
/*---------------------- value type -------------------------*/
#define TREEVTYPE TreeVTYPE
#define TreeVTYPE(ptree) (ptree->cm.vt)
/*----------------------- key type --------------------------*/
#define TREEKTYPE TreeKTYPE
#define TreeKTYPE(ptree) (ptree->cm.kt)
/*----------------- size of buffer system -------------------*/
#define TREEBSIZE TreeBSIZE
#define TreeBSIZE(n) a2SetNBuf(n)
/*------------- current transaction type --------------------*/
#define TREETRANS TreeTRANS
#define TreeTRANS(ptree) (ptree->pdf->trType1)


#ifdef __cplusplus
extern "C" {
#endif

#if (defined(__FOR_WINDOWS__) || defined(__FOR_WIN32__)) && defined(H_A2NIKA) && !defined(SOURCE_A2TREE)
# define a2trop(pce,file,disp,pblks,user) TreeOpen(pce,file,disp)
# define a2trcltr TreeClose
#else
 TREE A2FAR *a2trop (int *pce, char *file, int disp, int *pblks, void *user);
 int a2trcltr (TREE A2FAR *ptree);
#endif


//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#ifdef __DPMI32__
int a2trmove(TREE *pctree, TCOMMAND *pcommand);
int a2tr1c  (TREE *pt,char m,char a,char kt,void *k,char vt,void *v,char na);
int a2trcmp (void *p1, void *p2, char type);

#else

#ifndef H_A2NIKA

int a2trmove(TREE *pctree, TCOMMAND *pcommand);
int a2tr1c  (TREE *pt,char m,char a,char kt,void *k,char vt,void *v,char na);
int a2trcmp (void *p1, void *p2, char type);

#endif

#endif
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#ifdef __cplusplus
}
#endif

#endif /* A2DIO_SOURCE */

#endif /* H_A2TR */

